package kr.co.iniline.rmkb.core;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.collision.Ray;

public class ControlManager implements InputProcessor {
	PerspectiveCamera cam = null;
	
	public static final int KEY_EVENT = 1;
	public static final int TOUCH_EVENT = 2;
	
	public class TouchEvent {
		int x,y;
		float fx,fy;
		int btnClick = -1;
	}
	
	TouchEvent rTouchEvent = null;
	TouchEvent singleTouchEvent = new TouchEvent();
	
	public ControlManager() {
		cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		cam.near = 0.1f;
		cam.far = 100;
	}
	
	Vector3 getWorldPos(int mx, int my) {
		Ray ray = cam.getPickRay(mx, my);
		float t = (-cam.position.z) / ray.direction.z;
		Vector3 pos = ray.direction.mul(t).add(cam.position).cpy();
		return pos;
	}
	
	float cx,cy,cz;
	
	public void update(float delta) {
//		System.out.println( cam.position );
		
//		if( Gdx.input.getPitch() < -25f ) {
//			cam.translate(0f, 0.06f, 0f);
//		} else if( Gdx.input.getPitch() > 25f ) {
//			cam.translate(0f, -0.06f, 0f);
//		}
//		
//		if( Gdx.input.getRoll() < -25f ) {
//			if( cam.position.x > -5f ) {
//				cam.translate(-0.06f, 0f, 0f);
//			}
//		} else if( Gdx.input.getRoll() > 25f ) {
//			if( cam.position.x < 5f ) {
//				cam.translate(0.06f, 0f, 0f);
//			}
//		}
		
		cx=0f;
		cy=0f;
		cz=0f;
		if( Gdx.input.isKeyPressed( Input.Keys.LEFT ) ) {
			cx += 0.01f;
		} 
		if( Gdx.input.isKeyPressed( Input.Keys.RIGHT ) ) {
			cx += -0.01f;
		}
		if( Gdx.input.isKeyPressed( Input.Keys.UP) ) {
			cy += -0.01f;
		}
		if( Gdx.input.isKeyPressed( Input.Keys.DOWN) ) {
			cy += 0.01f;
		}
		if( Gdx.input.isKeyPressed( Input.Keys.HOME) ) {
			cz += 0.01f;
		}
		if( Gdx.input.isKeyPressed( Input.Keys.END) ) {
			cz += -0.01f;
		}
		
		
		cam.update();
		cam.apply(Gdx.gl10);
	}
	
//	public void reset() {
//		rTouchEvent = null;
//	}
//	
//	public TouchEvent getTouchEvent() {
//		return rTouchEvent;
//	}
	
	public void reset() {
		singleTouchEvent.btnClick = -1;
	}
	
	public TouchEvent getTouchEvent() {
		return singleTouchEvent;
	}
	
	@Override
	public boolean keyDown(int keycode) {
		if( Input.Keys.ENTER == keycode ) {
			System.out.println( "pos:"+cam.position + ", direction:"+cam.direction);
		} else if( Input.Keys.MENU == keycode ) {
			System.out.println( "Azimuth:"+Gdx.input.getAzimuth()+", Pitch:"+Gdx.input.getPitch()+", Roll:"+Gdx.input.getRoll()+", Rotation:"+Gdx.input.getRotation());
		}
		System.out.println(keycode+", "+Input.Keys.VOLUME_UP+", "+Input.Keys.VOLUME_DOWN+", "+Input.Keys.MEDIA_PREVIOUS+", "+Input.Keys.MEDIA_NEXT);
		if( Input.Keys.VOLUME_DOWN == keycode ) {
			cam.translate(0f, 0f, -0.06f);
			if( cam.position.z < -5f ) {
				cam.position.z = -5f;
			}
		} else if( Input.Keys.VOLUME_UP == keycode ) {
			cam.translate(0f, 0f, 0.06f);
			if( cam.position.z > 5f ) {
				cam.position.z = 5f;
			}
		}
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		return false;
	}
	
	int PREV_PICKED_CURR_GRID_X = -1;
	int PREV_PICKED_CURR_GRID_Y = -1;

	@Override
	public boolean touchDown(int x, int y, int pointer, int button) {
		// TODO Auto-generated method stub
		singleTouchEvent.btnClick = button;
		singleTouchEvent.x = x;
		singleTouchEvent.y = y;
		Vector3 wPos = getWorldPos(x, y);
		singleTouchEvent.fx = wPos.x;
		singleTouchEvent.fy = wPos.y;
		
//		if( backBorder == null ) return false;
//		if( myKubBox.onClick(x, y) ) {
//			
//		} else {
//			if( ON_GRID ) {
////				user.debugOwnKub();
//				ClientUser.Kub kubs[] = user.getSelectedKubs();
//				System.out.println("GRIDX:"+CURR_GRID_X+", GRIDY:"+CURR_GRID_Y);
//				/** regist kub */
//				if( kubs != null ) {
//					if( backBorder.setCubes(kubs, CURR_GRID_X, CURR_GRID_Y) ) {
////						user.removeKubs( kubs );
//					}
//				}
//				/** move kub */
//				else {
//					backBorder.moveCube(PREV_PICKED_CURR_GRID_X, 
//							PREV_PICKED_CURR_GRID_Y, 
//							CURR_GRID_X, 
//							CURR_GRID_Y,
//							true);
//					PREV_PICKED_CURR_GRID_X = CURR_GRID_X;
//					PREV_PICKED_CURR_GRID_Y = CURR_GRID_Y;
////					backBorder.cubes[CURR_GRID_X][CURR_GRID_Y]
//				}
//			}
//		}
		return false;
	}

	@Override
	public boolean touchUp(int x, int y, int pointer, int button) {
		return false;
	}

	@Override
	public boolean touchDragged(int x, int y, int pointer) {
		return true;
	}

	@Override
	public boolean touchMoved(int x, int y) {
		singleTouchEvent.x = x;
		singleTouchEvent.y = y;
		Vector3 wPos = getWorldPos(x, y);
		singleTouchEvent.fx = wPos.x;
		singleTouchEvent.fy = wPos.y;
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		if( amount > 0 ) {
			cam.position.z += 0.1f;
		} else {
			cam.position.z -= 0.1f;
		}
		return false;
	}
}
